package com.kehongborui.portal.config;

import com.alibaba.druid.pool.DruidDataSource;
import org.springframework.boot.autoconfigure.jdbc.DataSourceProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.sql.DataSource;

/**
 * @author zhang peng yang ming
 * @CopyRight (C) http://www.csdn.com
 * @brief
 * @email 122162005@qq.com
 * @date 2018-09-20
 */
@Configuration
// 该注解可以将DataSourceProperties注入到DruidConfig这个类的对象中
@EnableConfigurationProperties({DataSourceProperties.class, DruidProperties.class})
public class DruidConfig {

    @Bean
    public DataSource dataSource(DataSourceProperties properties, DruidProperties druidProperties) {
        DruidDataSource dataSource = new DruidDataSource();
        dataSource.setUrl(properties.getUrl());
        dataSource.setDriverClassName(properties.getDriverClassName());
        dataSource.setUsername(properties.getUsername());
        dataSource.setPassword(properties.getPassword());
        dataSource.setMaxActive(druidProperties.getMaxActive());
        dataSource.setMinIdle(druidProperties.getMinIdle());
        dataSource.setInitialSize(druidProperties.getInitialSize());
        dataSource.setMaxWait(druidProperties.getMaxWaitMillis());
        dataSource.setMinEvictableIdleTimeMillis(druidProperties.getMinEvictableIdleTimeMillis());
        return dataSource;
    }
}
